<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<HTML>
<HEAD>
<c:import url="/includes/header.jsp"></c:import>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/js/lib/jquery-tree/css/demo.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/js/lib/jquery-tree/css/zTreeStyle.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/lib/jquery-tree/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/lib/jquery-tree/js/jquery.ztree.excheck-3.5.js"></script>
</HEAD>
<BODY>
	<!-- 权限管理 -->
	<div class="modal-header">
		<input id="userId" name="userId" type="hidden" value="0">
		<button type="button" class="close" data-dismiss="modal"
			aria-label="Close">
			<span aria-hidden="true">&times;</span>
		</button>
		<h4 class="modal-title" id="myModalLabel">设置权限</h4>
	</div>
	<div class="content_wrap">
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		<div class="right">
			<ul class="info">
				<li class="title"><h2>注意:</h2>
					<ul class="list">
						<li class="highlight_red">请管理员谨慎分配菜单层级，不选择默认为最顶层菜单</li>
					</ul></li>
			</ul>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
		<button type="button" class="btn btn-primary btn-save">保存</button>
	</div>
</BODY>
</HTML>

<script type="text/javascript">
	$(function() {
		var setting = {
			check : {
				enable : true,
				chkStyle : "radio",
				radioType : "all"

			},
			data : {
				simpleData : {
					enable : true,
					idKey : "id",
					pIdKey : "pId",
					levelKey : "level",
					rootPId : ""
				}
			}
		};

		var userId = $("#userId").val();
		var url = "${pageContext.request.contextPath}/admin/resource/tree.do";
		$.ajax({
			url : url,
			type : "post",
			dataType : "json",
			data : {
				"userId" : userId
			},
			async : true,
			success : function(data) {
				if (data.code == "0") {
					console.info(data.data);
					var zNodesTree = data.data;
					var zTree = $("#treeDemo")
					$.fn.zTree.init(zTree, setting, zNodesTree);
				}
			},
			error : function() {
				bootbox.alert("网络不稳定,保存失败");
			}
		});

		//保存
		$(".btn-save").click(function() {
			var userId = $("#userId").val();
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getCheckedNodes(true);
			var ids = new Array();
			var names = new Array();
			$.each(nodes, function(n, value) {
				if (value) {
					ids.push(value.id);
					names.push(value.name);
				}
			});
			var resourcesId = ids.join(",");
			var resourcesName = names.join(",");
			$("#parentId").val(resourcesId);
			$("#content").val(resourcesName);
			myModalHidden();
		});

		var myModalHidden = function() {
			$('#myModal').hide();
			$(".modal-backdrop").remove();
		}

	});
</script>